package com.java.date;

import java.util.Calendar;

public class CalendarTest {
    Calendar calendar = Calendar.getInstance();

    public void test1() {
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        int weekday = calendar.get(Calendar.DAY_OF_WEEK);

        System.out.println("now is: " + year + "年" + month + "月" + day + "日" + hour + "时" + minute + "分" + second + "秒"
        + "星期" + weekday);
    }

    // 一年后的今天
    public void test2() {
        calendar.add(Calendar.YEAR, 1);

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println("one year later is: " + year + "年" + month + "月" + day + "日");
    }

    // 获取6月的最后一天
    public void test3() {
        int currentMonth = 2;
        calendar.set(calendar.get(Calendar.YEAR), currentMonth, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        // 获取日
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(currentMonth + "月的最后一天为" + day + "号");
    }

    public static void main(String[] args) {
        CalendarTest calendarTest = new CalendarTest();
        calendarTest.test1();
        System.out.println("==================");
        calendarTest.test2();
        System.out.println("==================");
        calendarTest.test3();
    }
}
